﻿#include "coleditrecorddialog.h"
#include "ui_coleditrecorddialog.h"
#include <QSqlQuery>
#include <QDate>
#include <QDebug>

int COLeditrecordDialog::ID;
COLeditrecordDialog::COLeditrecordDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::COLeditrecordDialog)
{
    ui->setupUi(this);
    ui->frame->resize(this->geometry().width(), ui->frame->height());
    ui->status->setText("添加信息状态：");
    ui->lenddate->setDate(QDate::currentDate());
    ui->returndate->setDate(QDate::currentDate());
    connect(ui->cancelbtn,SIGNAL(clicked(bool)),this,SLOT(close()));
}

COLeditrecordDialog::~COLeditrecordDialog()
{
    delete ui;}

void COLeditrecordDialog::receivestr(QStringList list)
{
ID=list.at(0).toInt();
QDate date1=QDate::fromString(list.at(1),"yyyy-MM-dd");
if(!list.at(2).isEmpty()){
QDate date2=QDate::fromString(list.at(2),"yyyy-MM-dd");
ui->returndate->setDate(date2);
ui->checkBox->click();}
ui->lenddate->setDate(date1);
ui->readman->setText(list.at(3));
ui->readunit->setText(list.at(4));
ui->object->setText(list.at(5));
ui->objectunit->setText(list.at(6));
ui->others->setText(list.at(8));
ui->content->setText(list.at(7));
}

void COLeditrecordDialog::on_ackbtn_clicked()
{
    if(ui->object->text().isEmpty()||ui->objectunit->text().isEmpty()||ui->content->text().isEmpty()||ui->readunit->text().isEmpty()||ui->readman->text().isEmpty()){
        ui->status->setText("修改信息状态:阅档日期，阅档人及单位，阅档对象及单位不能为空！");}
    else {
    QSqlQuery query;
    query.prepare("update ZZB.recordall set lenddate=?,returndate=?,lendman=?,lendunit=?,object=?,objectunit=?,content=?,others=? where recordID = ?");
    query.addBindValue(ui->lenddate->text());
    if(ui->checkBox->isChecked()){query.addBindValue(ui->returndate->text());}
    else{query.addBindValue(QVariant::Invalid);}
    query.addBindValue(ui->readman->text());
    query.addBindValue(ui->readunit->text());
    query.addBindValue(ui->object->text());
    query.addBindValue(ui->objectunit->text());
    query.addBindValue(ui->content->text());
    query.addBindValue(ui->others->text());
    query.addBindValue(ID);
    bool ok=query.exec();
    if(ok)
    {ui->status->setText("修改信息状态:修改成功");
    this->close();}
    else
    {ui->status->setText("修改信息状态:修改失败");}
}
}

void COLeditrecordDialog::on_checkBox_clicked()
{
    if(ui->checkBox->isChecked()){
        ui->returndate->setEnabled(true);
    }else{ui->returndate->setEnabled(false);}
}
